package com.sm.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sm.model.Dynamic;
import com.sm.model.DynamicComment;
import com.sm.model.LikeDynamic;
import com.sm.model.User;
import com.sm.service.DynamicCommentService;
import com.sm.service.DynamicService;
import com.sm.service.LikeDynamicService;
import com.sm.service.UserService;
import com.sm.util.Msg;
import com.sm.util.PageResult;
import com.sm.vo.DynamicCommentVo;
import com.sm.vo.FirstDynamicCommentVo;
import com.sm.vo.UserDynamicVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 动态圈
 */

@Controller
@RequestMapping("/dynamic")
public class DynamicController {
    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private DynamicCommentService dynamicCommentService;
    @Autowired
    private LikeDynamicService likeDynamicService;
    @Autowired
    private UserService userService;

    /**
     * 动态发布
     *
     * @param dynamic
     * @return
     */
    @RequestMapping("/addDynamicPost")
    @ResponseBody
    public Msg addDynamicPost(Dynamic dynamic) throws Exception {
        if (StrUtil.isEmpty(dynamic.getContent()) && dynamic.getUrlList() == null && dynamic.getVideoId() == null) {
            return Msg.fail().add("msg", "动态内容不能为空");
        } else {
            int i = dynamicService.addDynamicPost(dynamic);
            if (i == 1) {
                return Msg.success().add("msg", "动态发布成功");
            }
        }
        return Msg.fail().add("msg", "动态发布失败");
    }

    /**
     * 查询动态详情
     * @param dynamicId
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectDynamicById")
    public Msg selectDynamicById(Integer dynamicId,Integer userId) {
        //获取动态的详细信息
        Dynamic dy = dynamicService.selectdynamicById(dynamicId);
        String url = dy.getUrl();
        String[] split = url.split(",");
        List<String> strings = new ArrayList<>();
        for (String s : split) {
            strings.add(s);
        }
        dy.setUrlList(strings);
        //更改动态的阅读次数
        Dynamic dyn = new Dynamic();
        dyn.setDynamicId(dy.getDynamicId());
        dyn.setReadNum(dy.getReadNum() + 1);
        dynamicService.updateDynamic(dyn);

        //判断动态是否点赞
        List<LikeDynamic> likeExist = likeDynamicService.isExist(dynamicId, userId);
        Integer isLike = 0;
        if (ObjectUtil.isNotEmpty(likeExist)){
            isLike = 1;
        }


        return Msg.success().add("dy", dy).add("isLike",isLike);
    }

    /**
     * 动态详情页---点赞人列表
     */
    @RequestMapping("/getLikeUserList")
    @ResponseBody
    public Msg getLikeUserList(Integer dynamicId){
        List<LikeDynamic> likeDynamics = likeDynamicService.selectLikeUserIds(dynamicId);
        List<String> names = new ArrayList<>();
        for (LikeDynamic like : likeDynamics) {
            User user = userService.selectAvatarAndNickName(like.getLikeUser());
            if (ObjectUtil.isNotEmpty(user)){
                names.add(user.getNickName());
            }
        }
        return Msg.success(names);
    }

    /**
     * 查询动态列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectDynamic")
    public Msg selectDynamic(Integer currentPage,Integer pageSize,Integer userId) {
        List<Dynamic> results = new ArrayList<>();
        PageResult selectdynamic = dynamicService.selectdynamic(currentPage, pageSize);
        List<Dynamic> rows = selectdynamic.getRows();
        for (Dynamic r : rows) {
            User user = userService.selectAvatarAndNickName(r.getUserId());
            List<LikeDynamic> exist = likeDynamicService.isExist(r.getDynamicId(), userId);
            if (ObjectUtil.isNotEmpty(exist)){
                r.setIsLike(1);
            }else {
                r.setIsLike(0);
            }
            if (ObjectUtil.isNotEmpty(user)){
                r.setNickName(user.getNickName());
                r.setHeadLink(user.getHeadLink());
            }else{
                r.setNickName("不存在");
                r.setHeadLink("用户不存在");
            }
            results.add(r);
        }
        PageResult pageResult = new PageResult(Long.valueOf(results.size()), results);
        return Msg.success().add("selectdynamic",pageResult);
    }

    /**
     * 查询动态评论回复
     * @param dynamicId
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectDynamicCommentById")
    public Msg selectDynamicCommentById(@RequestParam("dynamicId")Integer dynamicId) {
        DynamicCommentVo result = new DynamicCommentVo();
        List<FirstDynamicCommentVo> firstDynamicCommentVos = new ArrayList<>();
        //查询一级评论
        List<DynamicComment> dynamicComments = dynamicCommentService.selectFirstDynamicCommentsById(dynamicId);
        for (DynamicComment comment : dynamicComments) {
            User user = userService.selectAvatarAndNickName(comment.getCommentUser());
            if (ObjectUtil.isNotEmpty(user)){
                comment.setExt1(user.getHeadLink());
            }
            FirstDynamicCommentVo firstDynamicCommentVo1 = dynamicCommentService.selectSecondComments(dynamicId, comment.getId(),comment.getId());
            firstDynamicCommentVo1.setFirstComment(comment);
            firstDynamicCommentVos.add(firstDynamicCommentVo1);
        }
        result.setDynamicId(dynamicId);
        result.setFirstDynamicCommentVos(firstDynamicCommentVos);
        return Msg.success().add("result",result);
    }

    /**
     * 根据动态的ID拉取此动态的评论回复记录
     * @param dynamicId
     * @return
     */
    @RequestMapping("/selectCommentByDynamic")
    @ResponseBody
    public Msg selectCommentByDynamic(@RequestParam("dynamicId") Integer dynamicId) {


        List<Map> mapList = dynamicService.selectCommentByDynamic(dynamicId);

        if (ObjectUtils.isEmpty(mapList)) {
            return Msg.success().add("msg", "没有评论回复");
        }
        return Msg.success().add("msg", "拉取评论回复成功").add("data", mapList);

    }

    /**
     * 根据动态的ID删除此条动态（假删除不再显示）同时删除所有的评论和回复点赞记录
     * @param dynamicId
     * @return
     */
    @RequestMapping("/deleteCommentByDynamic")
    @ResponseBody
    public Msg deleteCommentByDynamic(@RequestParam("dynamicId") Integer dynamicId) {

        if (dynamicId == null) {
            return Msg.fail().add("msg", "参数不为空！");
        }
        dynamicService.deleteCommentByDynamic(dynamicId);

        return Msg.success().add("msg", "删除成功");

    }
    /**
     *查询阅读人数
     * @param dynamicId
     * @return
     */
    @RequestMapping("/selectUserNumber")
    @ResponseBody
    public Msg selectUserNumber(@RequestParam("dynamicId")Integer dynamicId) {

        int i = dynamicCommentService.selectUserNumber(dynamicId);
        if(i<0){
            return Msg.fail();
        }
        return Msg.success().add("i",i);
    }

}
